<template>
	<view class="content">
		<view class="header">
			<view class="title">欢迎登录致清坊</view>
		</view>

		<view class="login-container">
			<button 
				v-if="checkboxValue1" 
				open-type="getPhoneNumber" 
				@getphonenumber="login"
				class="login-btn"
			>
				<text class="btn-text">授权手机号进行 登录/注册</text>
			</button>
			<view 
				v-else 
				class="login-btn disabled" 
				@click="showAgreementTip"
			>
				<text class="btn-text">授权手机号进行 登录/注册</text>
			</view>

			<view class="agreement-container">
				<checkbox-group @change="checkboxChange">
					<label class="checkbox-label">
						<checkbox :checked="checkboxValue1" color="#339966" style="transform:scale(0.8);margin-right: 10rpx;"/>
					</label>
				</checkbox-group>
				<text class="agreement-text">我已阅读并同意</text>
				<text class="agreement-link" @click="fun()">用户隐私协议</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		login_weChatMiNIAppLogin,
		common_getWeChatMiNIAppUserInfo,
		login_generateJWTToken
	} from '@/api/login.js'
	export default {
		data() {
			return {
				checkboxValue1: false, 
			}
		},
		onLoad(option) {
			console.log(option)
		},
		methods: {
			fun(){
				uni.navigateTo({
					url:'/pages/yhysxy'
				})
			},
			// 添加复选框变化事件
			checkboxChange(e) {
				console.log(this.checkboxValue1)
				this.checkboxValue1 = !this.checkboxValue1;
			},
			showAgreementTip() {
				uni.showToast({
					title: "请先同意用户协议",
					icon: 'none'
				})
			},
			login(e) {

				if (!e.detail.code) {
					uni.showToast({
						title: "登录失败",
						icon: "error"
					})
					return
				}
				
				uni.showLoading({
					title: '登录中...'
				})
				
				uni.login({
					provider: 'weixin',
					success: (res) => {
						if (res.code) {
							console.log('登录凭证:', res);
							// 将code发送到服务器，换取openid和session_key
							login_weChatMiNIAppLogin({code:res.code}).then(ro => {
								if (ro.code == 200) {
									const openid = ro.content.data.openid
									uni.setStorageSync('openid', openid)
									common_getWeChatMiNIAppUserInfo({
										open_id: openid,
										code: e.detail.code
									}).then(pr => {
										if (pr.code == 200) {
											login_generateJWTToken({
												open_id: openid,
												user_phone: pr.content.data.phone_info.phoneNumber,
												nick_name: "用户" + Math.random().toString(36).substr(2, 6)
											}).then(rt => {
												uni.hideLoading()
												if (rt.code == 200) {
													uni.setStorageSync("token", rt.content.token)
													uni.showToast({
														title: "登录成功!",
														icon: "success"
													})
													setTimeout(() => {
														uni.switchTab({url: '/pages/my'});
													}, 1500)
												}
											}).catch(() => {
												uni.hideLoading()
											})
										}
									}).catch(() => {
										uni.hideLoading()
									})
								}
							}).catch(() => {
								uni.hideLoading()
							})
						} else {
							uni.hideLoading()
							console.error('登录失败:', res.errMsg);
						}
					},
					fail: (err) => {
						uni.hideLoading()
						console.error('登录失败:', err);
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f8f8f8;
		padding: 0 40rpx;
		box-sizing: border-box;
	}
	
	.header {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.title {
		font-size: 56rpx;
		font-weight: bold;
		color: #07c160;
		text-align: center;
		line-height: 1.5;
	}
	
	.login-container {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		padding-top: 80rpx;
	}
	
	.login-btn {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #07c160;
		color: white;
		border-radius: 45rpx;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: none;
		margin-bottom: 40rpx;
		box-shadow: 0 10rpx 20rpx rgba(7, 193, 96, 0.2);
		
		&:active {
			opacity: 0.9;
			transform: translateY(2rpx);
		}
		
		&.disabled {
			background-color: #cccccc;
			box-shadow: none;
		}
	}
	
	.btn-text {
		font-weight: 500;
	}
	
	.agreement-container {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 40rpx;
		font-size: 26rpx;
		color: #666;
	}
	
	.agreement-text {
		margin-left: 10rpx;
	}
	
	.agreement-link {
		color: #07c160;
		margin-left: 8rpx;
		text-decoration: underline;
	}
	.checkbox-label {
	    display: flex;
	    align-items: center;
	    font-size: 13px;
	    font-weight: 400;
	}
</style>